﻿using System;
using System.Windows.Forms;
using System.Data;
using Gencode.PrsCommon.Data.Person;
using System.ComponentModel;

namespace Gencode.PrsUtil.Controls
{
	public class PUPersonGrid : Gencode.Gengui.GUGrid
	{
		DataView m_personen;

		private System.Windows.Forms.DataGridViewTextBoxColumn prsid;
		private System.Windows.Forms.DataGridViewTextBoxColumn anzeigename;
		private System.Windows.Forms.DataGridViewTextBoxColumn vorname;
		private System.Windows.Forms.DataGridViewTextBoxColumn nachname;
		private System.Windows.Forms.DataGridViewTextBoxColumn strasse;
		private System.Windows.Forms.DataGridViewTextBoxColumn strnr;
		private System.Windows.Forms.DataGridViewTextBoxColumn postfach;
		private System.Windows.Forms.DataGridViewTextBoxColumn plz;
		private System.Windows.Forms.DataGridViewTextBoxColumn ort;
		private System.Windows.Forms.DataGridViewTextBoxColumn telp;
		private System.Windows.Forms.DataGridViewTextBoxColumn teln;
		private System.Windows.Forms.DataGridViewTextBoxColumn email;


		public PUPersonGrid()
			: base()
		{
			InitializeLayout();

            this.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(PUPersonGrid_DataBindingComplete);
		}

        void PUPersonGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            for (int i = 0; i < Rows.Count; i++)
            {
                if (m_personen[i]["typ"].ToString() == "3")
                    Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Red;
                else if (m_personen[i]["typ"].ToString() == "2")
                    Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Gray;
            }
        }

		protected override void Dispose(bool disposing)
		{
			if (m_personen != null)
				m_personen.Dispose();

			base.Dispose(disposing);
		}

		protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
		{
			base.OnDataBindingComplete(e);
		}

		protected override void InitLayout()
		{
			base.InitLayout();
			this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
		}

        protected void InitializeLayout()
        {
            if (Columns.Count > 0)
                Columns.Clear();
            this.AutoGenerateColumns = false;

            this.prsid = new DataGridViewTextBoxColumn();
            this.anzeigename = new DataGridViewTextBoxColumn();
            this.vorname = new DataGridViewTextBoxColumn();
            this.nachname = new DataGridViewTextBoxColumn();
            this.strasse = new DataGridViewTextBoxColumn();
            this.strnr = new DataGridViewTextBoxColumn();
            this.postfach = new DataGridViewTextBoxColumn();
            this.plz = new DataGridViewTextBoxColumn();
            this.ort = new DataGridViewTextBoxColumn();
            this.telp = new DataGridViewTextBoxColumn();
            this.teln = new DataGridViewTextBoxColumn();
            this.email = new DataGridViewTextBoxColumn();

            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
				this.prsid,
		this.anzeigename,
		this.vorname,
		this.nachname,
		this.strasse,
		this.strnr,
		this.postfach,
		this.plz,
		this.ort,
		this.telp,
		this.teln,
		this.email});

            this.prsid.HeaderText = "ID";
            this.prsid.Name = "prsid";
            this.prsid.ReadOnly = true;
            this.prsid.Width = 43;
            this.prsid.Visible = ShowPrsID;

            this.anzeigename.HeaderText = "Name";
            this.anzeigename.Name = "anzeigename";
            this.anzeigename.ReadOnly = true;
            this.anzeigename.Visible = ShowAnzeigename;

            this.vorname.HeaderText = "Vorname";
            this.vorname.Name = "vorname";
            this.vorname.ReadOnly = true;
            this.vorname.Visible = ShowName;

            this.nachname.HeaderText = "Nachname";
            this.nachname.Name = "nachname";
            this.nachname.ReadOnly = true;
            this.nachname.Visible = ShowName;

            this.strasse.HeaderText = "Strasse";
            this.strasse.Name = "strasse";
            this.strasse.ReadOnly = true;
            this.strasse.Visible = ShowStrasse;

            this.strnr.HeaderText = "StrNr";
            this.strnr.Name = "strnr";
            this.strnr.ReadOnly = true;
            this.strnr.Visible = ShowStrasse;

            this.postfach.HeaderText = "Postfach";
            this.postfach.Name = "postfach";
            this.postfach.ReadOnly = true;
            this.postfach.Visible = ShowPostfach;

            this.plz.HeaderText = "PLZ";
            this.plz.Name = "plz";
            this.plz.ReadOnly = true;
            this.plz.Visible = ShowOrt;

            this.ort.HeaderText = "Ort";
            this.ort.Name = "ort";
            this.ort.ReadOnly = true;
            this.ort.Visible = ShowOrt;

            this.telp.HeaderText = "Telefon P.";
            this.telp.Name = "telp";
            this.telp.ReadOnly = true;
            this.telp.Visible = ShowTelefonPrivat;

            this.teln.HeaderText = "Telefon N.";
            this.teln.Name = "teln";
            this.teln.ReadOnly = true;
            this.teln.Visible = ShowNatel;

            this.email.HeaderText = "E-Mail";
            this.email.Name = "email";
            this.email.ReadOnly = true;
            this.email.Visible = ShowEmail;

            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
        }

		public void InitData(string mandid, PersonTyp typ)
		{
			Typ = typ;
			Mandid = mandid;
			ReloadData();
		}

		public void ReloadData()
		{
			if (m_personen != null)
				m_personen.Dispose();

			m_personen = PersonData.GetPersonen(Mandid, Typ);
			BindData();
		}

		private void BindData()
		{
			AutoGenerateColumns = false;
			DataSource = m_personen;

			Columns["prsid"].DataPropertyName = "prsid";
			Columns["anzeigename"].DataPropertyName = "anzeigename";
			Columns["vorname"].DataPropertyName = "vorname";
			Columns["nachname"].DataPropertyName = "nachname";
			Columns["strasse"].DataPropertyName = "strasse";
			Columns["strnr"].DataPropertyName = "strnr";
			Columns["postfach"].DataPropertyName = "postfach";
			Columns["plz"].DataPropertyName = "plz";
			Columns["ort"].DataPropertyName = "ort";
			Columns["telp"].DataPropertyName = "telp";
			Columns["teln"].DataPropertyName = "teln";
			Columns["email"].DataPropertyName = "email";

			this.prsid.Visible = ShowPrsID;
			this.anzeigename.Visible = ShowAnzeigename;
			this.vorname.Visible = ShowName;
			this.nachname.Visible = ShowName;
			this.strasse.Visible = ShowStrasse;
			this.strnr.Visible = ShowStrasse;
			this.postfach.Visible = ShowPostfach;
			this.plz.Visible = ShowOrt;
			this.ort.Visible = ShowOrt;
			this.telp.Visible = ShowTelefonPrivat;
			this.teln.Visible = ShowNatel;
			this.email.Visible = ShowEmail;
		}

		public int SelectedPrsID
		{
			get
			{
				int index = SelectedRows[0].Index;
				if (index >= 0)
					if (m_personen[index].Row["prsid"].ToString() != null &&
						m_personen[index].Row["prsid"].ToString() != string.Empty)
						return int.Parse(m_personen[index].Row["prsid"].ToString());

				return 0;
			}
		}

		PersonTyp m_typ = PersonTyp.Stammperson;
		public PersonTyp Typ
		{
			get
			{
				return m_typ;
			}
			set
			{
				m_typ = value;
			}
		}

		string m_mandid = "";
		public string Mandid
		{
			private get
			{
				return m_mandid;
			}
			set
			{
				m_mandid = value;
			}
		}

		bool m_showname = true;
		public bool ShowName
		{
			get
			{
				return m_showname;
			}
			set
			{
				m_showname = value;
			}
		}

		bool m_showprsid = true;
		public bool ShowPrsID
		{
			get
			{
				return m_showprsid;
			}
			set
			{
				m_showprsid = value;
			}
		}

		public bool ShowStrasse
		{
			get;
			set;
		}

		public bool ShowPostfach
		{
			get;
			set;
		}

		public bool ShowOrt
		{
			get;
			set;
		}

		public bool ShowTelefonPrivat
		{
			get;
			set;
		}

		public bool ShowNatel
		{
			get;
			set;
		}

		public bool ShowEmail
		{
			get;
			set;
		}

		public bool ShowAnzeigename
		{
			get;
			set;
		}
	}
}
